package cn.coopen.italktv.upgradeslencedemo;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	String localFilePath = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? (Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "apkfile") : "";
	public void onSilentInstall(View view){
		if (isRoot()){
			Toast.makeText(MainActivity.this,"开始升级",Toast.LENGTH_SHORT).show();
			new Thread(){
				@Override
				public void run() {
					SilentInstall installHelper = new SilentInstall();
					File apkPath = new File(localFilePath);
					File[] apks = apkPath.listFiles();
					Log.d(TAG, "run: 安装数量："+apks.length);
					for (int i = 0; i < apks.length; i++) {
						String pm = apks[i].getAbsolutePath();
					/*	boolean install = installHelper.install(pm);*/
						String install = installHelper.installApkSilent(apks[i]);
						Log.d(TAG,pm.concat("安装成功：") + install);
					}

				}
			}.start();
		}else {
			Toast.makeText(MainActivity.this,"没有权限", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 判断手机是否拥有Root权限。
	 * @return 有root权限返回true，否则返回false。
	 */
	public boolean isRoot() {
		boolean bool = false;
		try {
			bool = new File("/system/bin/su").exists() || new File("/system/xbin/su").exists();
		} catch (Exception e) {
			e.printStackTrace();
		}
//		return bool;
		return true;
	}
}
